package com.bear.newsset.widget;

import android.content.Context;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import android.util.AttributeSet;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

import com.bear.newsset.R;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FixedGridView extends GridView {
    private List<Map<String, Object>> mData;
    //Item数据类型
    private String[] itemFrom = { "ItemImg", "ItemTitle", "ItemContent" };
    //Item数据对应的布局
    private int[] itemRes = {
            R.id.item_grid_action_iv_img,
            R.id.item_grid_action_tv_title,
            R.id.item_grid_action_tv_content
    };

    private SimpleAdapter mAdapter;

    public FixedGridView(Context context) {
        super(context);
        init();
    }

    public FixedGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FixedGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mData = new ArrayList<>();
    }

    /**
     * 设置数据
     * @param listData      数据源
     */
    public void setData(@NonNull List<FixedGridEntity> listData) {
        //更新数据源
        updateData( listData );
        mAdapter = new SimpleAdapter(
                getContext(), mData, R.layout.item_grid_action, itemFrom, itemRes
        );
        //设置Grid适配器
        setAdapter( mAdapter );
    }

    /**
     * 更新数据源
     * @param listData      数据源
     */
    public void updateData(@NonNull List<FixedGridEntity> listData) {
        mData.clear();
        //数据转换
        for( FixedGridEntity e : listData ) {
            Map<String, Object> itemData = new HashMap<>();
            //图标
            itemData.put(itemFrom[0], e.getImg());
            //标题
            itemData.put(itemFrom[1], e.getTitle());
            //内容
            itemData.put(itemFrom[2], e.getContent());
            mData.add( itemData );
        }
        notifyDataSetChanged();
    }

    /**
     * 刷新数据
     */
    public void notifyDataSetChanged() {
        if( mAdapter != null ) mAdapter.notifyDataSetChanged();
    }

    /**
     * 建议使用自带的{@link #setData(List)}
     * @param adapter   适配器
     */
    @Deprecated
    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, height);
    }

    public static class FixedGridEntity {
        @DrawableRes
        private int img;
        private String title;
        private String content;
        @NotNull
        @Override
        public String toString() {
            return "FixedGridEntity{" +
                    "img=" + img +
                    ", title='" + title + '\'' +
                    ", content='" + content + '\'' +
                    '}';
        }
        public int getImg() {
            return img;
        }
        public FixedGridEntity setImg(int img) {
            this.img = img;
            return this;
        }
        public String getTitle() {
            return title;
        }
        public FixedGridEntity setTitle(String title) {
            this.title = title;
            return this;
        }
        public String getContent() {
            return content;
        }
        public FixedGridEntity setContent(String content) {
            this.content = content;
            return this;
        }
    }
}
